package storage.v1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class RawTextReference {
	
	private String name, type ;	
	private String filePath ;//phisical path
	
	
	//arrays<String>
	private HashMap<String,RawTextReference> childrenMap ;
	private HashMap<String,RawTextReference> parentsMap ;
	
	public RawTextReference() {
		super();
	}
	
	public RawTextReference(String name, String type, String filePath) {
		super();
		this.name = name;
		this.type = type;
		this.filePath = filePath;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getName() {
		return name;
	}

	public Map<String,RawTextReference> getChildren() {
		
		checkMaps() ;
		return childrenMap;
	}
	public Map<String,RawTextReference> getParents() {
		
		checkMaps() ;
		return parentsMap;
	}
	
	
	public Collection<String> getChildrenId() {
		
		checkMaps() ;
		return childrenMap != null ? childrenMap.keySet() : new ArrayList<String>() ;
	}
	
	public Collection<String> getParentsId() {
		
		checkMaps() ;
		return parentsMap != null ? parentsMap.keySet() : new ArrayList<String>() ;
	}

	public void addChild( RawTextReference ref ) {
		
		if( ref == null )
			return ;
		
		if( ref.getName().equals(this.getName()) )
			return ;
		
		if( this.childrenMap == null )
			this.childrenMap = new HashMap<String, RawTextReference>() ;
		
		this.childrenMap.put( ref.getName(), ref ) ;
	}
	
	public void addParent( RawTextReference ref ) {
		
		if( ref == null )
			return ;
		
		if( ref.getName().equals(this.getName()) )
			return ;
		
		if( this.parentsMap == null )
			this.parentsMap = new HashMap<String, RawTextReference>() ;
		
		this.parentsMap.put( ref.getName(), ref ) ;
	}
	
	private void checkMaps()
	{
		if( parentsMap != null )
			parentsMap.remove( this.getName() ) ;
		if( childrenMap != null )
			childrenMap.remove( this.getName() ) ;
	}

	public void setName(String name) {
		this.name = name ;		
	}
}
